﻿<%@ WebHandler Language="C#" Class="ImageHandler" %>

using System;
using System.Web;

public class ImageHandler : IHttpHandler {

    System.Net.WebClient wc = new System.Net.WebClient();
    
    public void ProcessRequest (HttpContext context) {
        if (context.Request.QueryString["url"] != null)
        {
            byte[] responseArray;
            string url = context.Request.QueryString["url"];
            url = "http://mh2.xindm.cn/book/h/naruto/Act_597zs/000001.jpg";
            //if (context.Request.QueryString["id"] != null)
            //{
            //    filename = "/cache/" + context.Request.QueryString["id"] + url.Substring(url.LastIndexOf("/"));
            //    if (System.IO.Directory.Exists(context.Server.MapPath("~/cache/" + context.Request.QueryString["id"])) == false)//如果不存在就创建文件夹
            //    {
            //        System.IO.Directory.CreateDirectory(context.Server.MapPath("~/cache/" + context.Request.QueryString["id"]));
            //    }
            //    else
            //    {
            //        if (System.IO.File.Exists(context.Server.MapPath("~" + filename)))
            //        {
            //            context.Response.Redirect(filename);
            //            context.Response.End();
            //        }
            //    }
            //}
            //else
            //{
                //filename = "/cache/" + YuuzleComic.Common.Input.MD5(url) + ".jpg";
                //if (System.IO.File.Exists(context.Server.MapPath("~" + filename)))
                //{
                //    context.Response.Redirect(filename);
                //    context.Response.End();
                //}
            //}

            //if (new Uri(url).Host == "xindm.cn")//如果是短域名链接
            //{
            //    url = url.Replace("xindm.cn", "www.xindm.cn");
            //}
            //if (wc.CookieContainer.GetCookies(new Uri(url))["virtualwall"] == null)//如果没有cookie
            //{
            //    wc.GetHtml("http://" + new Uri(url).Host);
            //}

            System.Net.CookieContainer cookies = new System.Net.CookieContainer();
            url = "http://mh2.xindm.cn/";
            System.Net.HttpWebRequest myHttpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            myHttpWebRequest.Timeout = 20 * 1000; //连接超时
            myHttpWebRequest.Accept = "*/*";
            myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0;)";
            myHttpWebRequest.CookieContainer = new System.Net.CookieContainer(); //暂存到新实例
            myHttpWebRequest.GetResponse().Close();
            cookies = myHttpWebRequest.CookieContainer; //保存cookies
            string cookiesstr = myHttpWebRequest.CookieContainer.GetCookieHeader(myHttpWebRequest.RequestUri);


            myHttpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("http://mh2.xindm.cn/book/0-9/360wmsj/act_11/xindm_cn_000001.jpg");//moviePath为图片的真实URL  
            myHttpWebRequest.Referer = "http://mh2.xindm.cn/";//伪造网址
            myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0;)";
            myHttpWebRequest.CookieContainer = cookies;
            System.Net.HttpWebResponse response = myHttpWebRequest.GetResponse() as System.Net.HttpWebResponse;
            System.IO.Stream responseStream = response.GetResponseStream();
            System.Drawing.Image img = System.Drawing.Image.FromStream(responseStream);
            img.Save("C:\\1.jpg");//保存到C盘
            responseStream.Close();
            response.Close();
            YuuzleComic.Common.XindmSession.saveImage("http://mh2.xindm.cn/book/h/naruto/Act_597zs/000001.jpg", "C:\\2.jpg");
            wc.Headers.Add(System.Net.HttpRequestHeader.Referer, "http://mh2.xindm.cn/display.asp?id=69153");
            if (Maticsoft.Common.DataCache.GetCache(url) != null)
            {
                responseArray = (byte[])Maticsoft.Common.DataCache.GetCache(url);
            }
            else
            {
                responseArray = wc.DownloadData(url);
                Maticsoft.Common.DataCache.SetCache(url, responseArray, DateTime.Now.AddMinutes(60), TimeSpan.Zero);
            }
            
            try
            {
                //id不为空才保存
                //if (context.Request.QueryString["id"] != null)
                //{
                //    System.IO.MemoryStream ms = new System.IO.MemoryStream(responseArray);
                //    System.Drawing.Image image = System.Drawing.Image.FromStream(ms);
                //    image.Save(context.Server.MapPath("~" + filename));
                //}
            }
            catch (Exception)
            {
            }
            //context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            context.Response.ClearContent();
            context.Response.ContentType = "image/Jpeg";
            context.Response.BinaryWrite(responseArray);
            context.Response.End();
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}